import { Record, Map } from 'immutable';
import { createList } from './node';
import Style from './style';
import generateKey from '../utils/generate-key';

const DEFAULTS = {
  data: undefined,
  key: undefined,
  nodes: undefined,
  style: undefined,
  object: undefined,
  type: undefined
};

class Document extends Record(DEFAULTS) {
  static create(attr = {}) {
    return Document.fromJSON(attr);
  }

  static fromJSON(object) {
    if (Document.isDocument(object)) {
      return object;
    }
    const {
      nodes = [],
      data = {},
      type = 'form',
      key = generateKey(),
      style = {}
    } = object;
    return new Document({
      nodes: createList(nodes),
      data: Map(data),
      key,
      type,
      style: Style.create(style),
      object: 'document'
    });
  }
}

Document.prototype.__document__ = true;
export default Document;
